package com.atguigu.mySmartCampus.controller;

import com.atguigu.mySmartCampus.Service.TeacherService;
import com.atguigu.mySmartCampus.entity.Teacher;
import com.atguigu.mySmartCampus.util.MD5;
import com.atguigu.mySmartCampus.util.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api("tags= 老师")
@RequestMapping("/sms/teacherController")
public class TeacherController{

    @Autowired
    private TeacherService teacherService;
    @DeleteMapping("/deleteTeacher")
    public Result deleteTeacher(@RequestBody List<Integer> ids){
        teacherService.removeByIds(ids);
        return Result.ok();
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpadate(@RequestBody Teacher teacher){
        Integer id = teacher.getId();
        if(id==null|| id==0) teacher.setPassword(MD5.encrypt(teacher.getPassword()));
        teacherService.saveOrUpdate(teacher);
        return Result.ok();
    }

    @GetMapping("getTeachers/{pageNo}/{pageSize}")
    public Result getTeacherByOperator(@ApiParam("页码") @PathVariable Integer pageNo,
    @ApiParam("页大小") @PathVariable Integer pageSize,
    @ApiParam("老师") Teacher teacher
    ){
        //.. 用不着 if(StringUtils.isEmpty(teacher.getName())) 
        Page<Teacher> page = new Page(pageNo,pageSize);
        IPage<Teacher> ipage = teacherService.getTeacherByOperator(page,teacher);
        return Result.ok(ipage);
    }
}